﻿// See https://aka.ms/new-console-template for more information
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
class Order
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Customer { get; set; }
    public double Number { get; set; }

    public Order(int id, string name, string customer, double number)
    {
        Id = id;
        Name = name;
        Customer = customer;
        Number = number;
    }

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return Id == other.Id && Name == other.Name && Customer == other.Customer && Number == other.Number;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return Id;
    }

    public override string ToString()
    {
        return $"Id: {Id}, Name: {Name}, Customer: {Customer}, Number: {Number}";
    }
}

class OrderDetails : Order
{
    public OrderDetails(int id, string name, string customer, double number)
        : base(id, name, customer, number)
    {
    }

   
}

class OrderService
{
    public List<Order> orders = new List<Order>();
    public OrderService()
    {
        orders = new List<Order>();
    }

    public void AddOrder(Order order)
    {
        if (!orders.Contains(order))
        {
            orders.Add(order);
        }
        else
        {
            throw new InvalidOperationException("订单已存在！");
        }
    }

    public void RemoveOrder(int orderId)
    {
        var orderToRemove = orders.FirstOrDefault(o => o.Id == orderId);
        if (orderToRemove != null)
        {
            orders.Remove(orderToRemove);
        }
        else
        {
            throw new InvalidOperationException("找不到指定的订单！");
        }
    }

    public void ModifyOrder(int orderId, string newName, double newNumber)
    {
        var orderToModify = orders.FirstOrDefault(o => o.Id == orderId);
        if (orderToModify != null)
        {
            orderToModify.Name = newName;
            orderToModify.Number = newNumber;
        }
        else
        {
            throw new InvalidOperationException("找不到指定的订单！");
        }
    }

    public List<Order> QueryOrdersByAmount()
    {
        return orders.OrderBy(o => o.Number).ToList();
    }

    public void SortOrdersBy(Func<Order, object> keySelector)
    {
        orders = orders.OrderBy(keySelector).ToList();
    }




    public void Export(string filePath)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream writer = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                serializer.Serialize(writer, orders);
            }

            Console.WriteLine("Orders exported successfully to XML file.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error exporting orders: " + ex.Message);
        }
    }


    public void Import(string filePath)
    {
        try
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);

            XmlNodeList orderNodes = xmlDoc.SelectNodes("//Order");

            // Clear existing orders before importing
            orders.Clear();

            foreach (XmlNode orderNode in orderNodes)
            {
                int id = int.Parse(orderNode.SelectSingleNode("Id").InnerText);
                string name = orderNode.SelectSingleNode("Name").InnerText;
                string customer = orderNode.SelectSingleNode("Customer").InnerText;
                double number = double.Parse(orderNode.SelectSingleNode("Number").InnerText);

                Order order = new Order(id, name, customer, number);
                orders.Add(order);
            }

            Console.WriteLine("Imported Orders:");
            foreach (var order in orders)
            {
                Console.WriteLine(order.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error importing orders: " + ex.Message);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var orderService = new OrderService();
        var order1 = new Order(1, "Product A", "Alice", 100.0);
        var order2 = new Order(2, "Product B", "Bob", 200.0);
        var order3 = new Order(3, "Product C", "Casey", 300.0);
        var order4 = new Order(4, "Product D", "David", 100.0);

        orderService.AddOrder(order1);
        orderService.AddOrder(order2);
        orderService.AddOrder(order3);
        orderService.AddOrder(order4);
        var sortedOrders1 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders by Amount:");
        foreach (var order in sortedOrders1)
        {
            Console.WriteLine(order.ToString());
        }

        try
        {
            orderService.RemoveOrder(1);//移除Alice
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.Message);
        }

        orderService.ModifyOrder(2, "Product C", 200.0);

        var sortedOrders2 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders after Modification:");
        foreach (var order in sortedOrders2)
        {
            Console.WriteLine(order.ToString());
        }

        orderService.SortOrdersBy(o => o.Customer);
        var sortedOrders3 = orderService.QueryOrdersByAmount();
        Console.WriteLine("Sorted Orders by Customer:");
        foreach (var order in sortedOrders3)
        {
            Console.WriteLine(order.ToString());
        }
        orderService.Export("C:\\Users\\ThinkPad\\Desktop\\vs\\5.1\\5.1\\XMLFile1.xml");

        
        orderService.Import("C:\\Users\\ThinkPad\\Desktop\\vs\\5.1\\5.1\\XMLFile1.xml");
    }

}
